package com.example.bootes.config;


import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.nio.reactor.IOReactorConfig;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.context.annotation.Bean;
import org.springframework.data.elasticsearch.core.ElasticsearchOperations;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;

/**
 * @author daxue0929
 * @date 2022/8/22
 */

//为了编译不报错，先取消此注解
//@Configuration
public class ESConfig {

//    final ElasticsearchProperties elasticsearchProperties;
//
//    @Autowired
//    public ESConfig(ElasticsearchProperties elasticsearchProperties) {
//        this.elasticsearchProperties = elasticsearchProperties;
//    }


    @Bean
    RestHighLevelClient restHighLevelClient() {


//        List<EsConfigProperty.Cluster> clusters = esConfigProperty.getClusters();
//        if(CollectionUtils.isEmpty(clusters)){
//            return null;
//        }
//        EsConfigProperty.Cluster cluster = clusters.stream().findFirst().get();
//        String host = hostPortName.substring(0, hostPortName.lastIndexOf(":"));
//        int port = Integer.parseInt(hostPortName.substring(hostPortName.lastIndexOf(":") + 1));
        // 初始化 RestClient, hostName 和 port 填写集群的内网 VIP 地址与端口
        RestClientBuilder builder = RestClient.builder(
                new HttpHost("9.135.75.7", 9200, "http"))
//                .setRequestConfigCallback(builder1 -> {
//                    builder1.setConnectTimeout(cluster.getConnTimeout());
//                    builder1.setSocketTimeout(cluster.getSocketTimeout());
//                    builder1.setConnectionRequestTimeout(cluster.getConnectionRequestTimeout());
//                    return builder1;
//                })
                ;
        //保活策略
        builder.setHttpClientConfigCallback(httpClientBuilder -> httpClientBuilder
                .setDefaultIOReactorConfig(IOReactorConfig.custom()
                        .setSoKeepAlive(true)
                        .build()));
//        // 设置认证信息
//        if(StringUtils.isNotEmpty(cluster.getUsername())&& StringUtils.isNotEmpty(cluster.getPassword())) {
//            //如果没配置密码就可以不用下面这两部
//            final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
//            credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(cluster.getUsername(), cluster.getPassword()));
//            builder.setHttpClientConfigCallback(httpAsyncClientBuilder -> {
//                httpAsyncClientBuilder.disableAuthCaching();
//                return httpAsyncClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
//            });
//        }

        CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
        credentialsProvider.setCredentials(AuthScope.ANY,
                new UsernamePasswordCredentials("elastic", "efBRj*7896dsHC"));
        builder.setHttpClientConfigCallback(f -> {
            f.disableAuthCaching();
            return f.setDefaultCredentialsProvider(credentialsProvider);
        });


        RestHighLevelClient client = new RestHighLevelClient(builder);
        return client;
    }

    @Bean
    public ElasticsearchOperations elasticsearchTemplate() {
        return new ElasticsearchRestTemplate(restHighLevelClient());
    }


}
